package com.android.fastride.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.fastride.R;
import com.android.fastride.contract.DispatchCountContract;
import com.android.fastride.present.DispatchCountPresent;
import com.android.fastride.view.widget.TopBar;
import com.peng.library.mantis.expand.data.MantisDataFragment;
import com.peng.library.mantis.hint.HintConfig;
import com.peng.library.mantis.mvp.BindPresent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 配送统计
 */
@BindPresent(DispatchCountPresent.class)
public class DispatchCountFragment extends MantisDataFragment<DispatchCountContract.PRESENT,
        Boolean> implements DispatchCountContract.VIEW {
    private static final String REQUEST_DATE = "request_date";
    private static final int REQUEST_CODE_START_DATE = 1;
    private static final int REQUEST_CODE_END_DATE = 2;
    private Unbinder unbinder;
    private FragmentManager fragmentManager;
    private View view;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM.dd.yyyy");

    @BindView(R.id.topbar)
    TopBar topbar;
    @BindView(R.id.tv_day_count)
    TextView tvDayCount;
    @BindView(R.id.tv_day_orders)
    TextView tvDayOrders;
    @BindView(R.id.tv_week_count)
    TextView tvWeekCount;
    @BindView(R.id.tv_week_orders)
    TextView tvWeekOrders;
    @BindView(R.id.tv_month_count)
    TextView tvMonthCount;
    @BindView(R.id.tv_month_orders)
    TextView tvMonthOrders;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.iv_pick_start_date)
    ImageView ivPickStartDate;
    @BindView(R.id.tv_end_date)
    TextView tvEndDate;
    @BindView(R.id.iv_pick_end_date)
    ImageView ivPickEndDate;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState, HintConfig.Builder hintConfigBuilder) {
        view = inflater.inflate(R.layout.fragment_dispatching_count, container, false);
        unbinder = ButterKnife.bind(this, view);

        fragmentManager = getFragmentManager();
        initView();
        return view;
    }

    private void initView() {
        topbar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {
            @Override
            public void OnLeftButtonClick() {
                getActivity().finish();
            }

            @Override
            public void OnRightButtonClick() {
            }
        });

        setTextStyle(tvDayCount, 540);
        setTextStyle(tvWeekCount, 3670.32f);
        setTextStyle(tvMonthCount, 10394.39f);
    }

    @Override
    public void onReceiveData(Boolean aBoolean) {

    }

    @Override
    public void onReceiveError(Throwable error) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @OnClick({R.id.tv_start_date, R.id.iv_pick_start_date, R.id.tv_end_date, R.id.iv_pick_end_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_start_date:
            case R.id.iv_pick_start_date:
                pickerDate(REQUEST_CODE_START_DATE, stringToDate(tvStartDate.getText().toString()));
                break;
            case R.id.tv_end_date:
            case R.id.iv_pick_end_date:
                pickerDate(REQUEST_CODE_END_DATE, stringToDate(tvEndDate.getText().toString()));
                break;
        }
    }

    private void pickerDate(int requestCode, Date date) {
        DatePickerFragment datePickerFragment = DatePickerFragment.newInstance(date);
        datePickerFragment.setTargetFragment(DispatchCountFragment.this, requestCode);
        datePickerFragment.show(fragmentManager, REQUEST_DATE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
        if (resultCode != Activity.RESULT_OK || date == null) {
            return;
        }

        if (requestCode == REQUEST_CODE_START_DATE) {
            tvStartDate.setText(dateToString(date));
        } else if (requestCode == REQUEST_CODE_END_DATE) {
            tvEndDate.setText(dateToString(date));
        }

    }

    public String dateToString(Date date) {
        return simpleDateFormat.format(date);
    }

    public Date stringToDate(String dateStr) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    private void setTextStyle(TextView view, int val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, float val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, String val) {
        String values = null;
        if (val.contains(".")) {
            values = val;
        } else {
            values = val + ".00";
        }
        int position = values.indexOf(".");
        SpannableString styleText = new SpannableString(values);
        styleText.setSpan(new TextAppearanceSpan(getContext(), R.style.bigGreenTextStyle), 0,
                position + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        styleText.setSpan(new TextAppearanceSpan(getContext(), R.style.smallGreenTextStyle),
                position + 1, values.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(styleText, TextView.BufferType.SPANNABLE);
    }
}
